var models = require('./models') ;    // 导入自定义组件
var Cp = models.cp;
var Pa = models.pa;
var users = models.users;

//通过手机浏览器查询产品信息
exports.getWeb = function(req,res){
    var barcode = req.param('barcode');
    Cp.findOne({sn:barcode},{'pn':1,'cd':1,'ds':1,'bn':1,'tp':1,'sds':1,'sps':1},function(e,doc){
        if (e) res.send(e.message);
        if (doc != null) {
            var tpSub = doc.tp.substr(0,3);
            res.render('index',{cp:doc,tpSub:tpSub,message:'1'});
        }else{
            res.send("没有查到该产品信息！");
        };
    });
};
//登录并查看产品的更多信息
exports.doLogAndFindCp = function(req,res){
    var un = req.body.username;
    var pw = req.body.password;
    users.count({un:un,pw:pw},function(err,user){
        var cpId = req.body.cpId;
        if(user == 1){
            Cp.findById(cpId,function(err,cp){
                res.render('result',{cp:cp,sds:cp.sds,sps:cp.sps});
            });
        }else{
            Cp.findOne({_id:cpId},{'pn':1,'cd':1,'ds':1,'bn':1,'tp':1,'sds':1,'sps':1},function(e,cp){
                var tpSub = cp.tp.substr(0,3);
                res.render('index',{cp:cp,tpSub:tpSub,message:'用户名或密码错误！'});
            });
        }
    });
};
//根据产品ID查看返修记录
exports.mobileFindMr = function(req,res){
    var cpId = req.body.cpId;
    Cp.findById(cpId,{'mrs':1},function(err,doc){
        if(err){
            res.send('0');
        }else if(doc.mrs.length==0){
            res.send('0');
        }else{
            res.send(doc.mrs);
        }
    })
};
//添加维修记录
exports.mAddMr = function(req,res){
    var cpId = req.body.cpId;
    var md = req.body.mrd;
    var ep = req.body.ep;
    var qp = req.body.qp;
    var wa = req.body.wa;
    var mrcon = req.body.mrcon;
    Cp.findById(cpId,function(err,cp){
        cp.mrs.push({md:md,me:ep,mqp:qp,mwa:wa,con:mrcon});
        cp.save(function(err){
            if (err) {
                res.send("0");
            }else{
                res.send("1");
            };
        });
    });
};
//删除维修记录
exports.mobileDeleteMr = function(req,res){
    var cpId = req.body.cpId;
    var mrId = req.body.mrId;
    Cp.findById(cpId,function(err,cp){
        cp.mrs.pull({_id:mrId});
        cp.save(function(err){
            if (err) {
                res.send("0");
            }else{
                res.send("1");
            };
        });
    });
};
//高级搜索
exports.mobileSearchCp = function(req,res){
    var sn = req.body.sn;
    var pn = req.body.pn;
    var tp = req.body.tp;
    var ds = req.body.ds;
    Cp.find({sn:new RegExp(sn),pn:new RegExp(pn),tp:new RegExp(tp),ds:new RegExp(ds)},function(err,docs){
        if (docs.length == 0) {
            res.send("0");
        }else{
            res.send(docs);
        };
    });
};
//查看产品详情
exports.mobileCpInfo = function(req,res){
    var cpId = req.param('cpId');
    Cp.findById(cpId,function(err,cp){
        res.render('result',{cp:cp,mrs:cp.mrs,sds:cp.sds,sps:cp.sps,pid:cp._id});
    });
};
//查询组件信息
exports.mobileFindAss = function(req,res){
    var paIds = req.body.ass;
    Pa.find({_id:{$in:paIds.split(',')}},function(err,PaArr){
        if(err){
            res.send("0");
        }else{
            res.send(PaArr);
        }
    });
};